package com.scau.yangsuiyu.estore.service;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.dababases.dao.ShopDao;
import com.scau.yangsuiyu.estore.dababases.entity.Shop;

import java.sql.Timestamp;

/**
 * @author suiyu_yang
 * @description 注册店铺的业务
 * @date 2019-06-08 20:21
 * @email suiyu_yang@163.com
 */
public class CreateShopService {
    private Shop shop;

    public CreateShopService(Shop shop) {
        this.shop = shop;
    }

    public JSONObject registerShop() {
        JSONObject resultJson = new JSONObject();

        if (ifShopNameExisted(shop.getShopName())) { //店铺名已存在
            resultJson.put("status", "failed");
            resultJson.put("result", "店铺名已存在！");
        } else {

            setDefaultValue();
            resultJson.put("status", "success");
            resultJson.put("result", "注册店铺成功！");
            resultJson.put("shop", shop);

            new ShopDao().insert(shop);
        }

        return resultJson;
    }

    /**
     * 设置默认值
     */
    private void setDefaultValue() {
        shop.setQualityScore(5);
        shop.setDeliveryScore(5);
        shop.setServiceScore(5);
        shop.setCreateTime(new Timestamp(System.currentTimeMillis()));
    }

    /**
     * 判断商铺名是否已存在
     *
     * @param shopName
     * @return 存在返回true，不存在返回false；
     */
    private boolean ifShopNameExisted(String shopName) {
        Shop shop = new ShopDao().getShopByName(shopName);
        if (shop == null) {
            return false;
        } else {
            return true;
        }
    }
}
